package com.hack.qiaoqiao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ContractListActivity extends Activity {
	private TextView people_name ;  
	private TextView people_phone;
	private CheckBox check_ok;
	private ListView contract_list;
	private ArrayList<String[]> persons = new ArrayList<String[]>();
	private ContractAdapter adapter;
	static final String[] PEOPLE_PROJECTION = {
		ContactsContract.Contacts._ID,
	    ContactsContract.Contacts.DISPLAY_NAME,
	    ContactsContract.Contacts.STARRED,
	    ContactsContract.Contacts.TIMES_CONTACTED,
	    ContactsContract.Contacts.CONTACT_PRESENCE,
	    ContactsContract.Contacts.PHOTO_ID,
	    ContactsContract.Contacts.LOOKUP_KEY,
	    ContactsContract.Contacts.HAS_PHONE_NUMBER,
		
	};
	static class ViewHolder {
	    public TextView people_name ;
	    public TextView people_phone ;
	    public ImageView image;
	    public CheckBox checkbox;
	  }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contract_list);
		contract_list = (ListView)findViewById(R.id.Phone_contract_list);
		LoadContract task = new LoadContract();
		task.execute(this.getApplicationContext());
		
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.contract_list, menu);
		return true;
	}
	
	class ContractAdapter extends BaseAdapter {
		private Map<String, String[]> Peoples = new HashMap<String,String[]>();
		private ArrayList<String> Names = new ArrayList<String>();
		private ArrayList<Boolean> MyCheckBox = new ArrayList<Boolean>();
		private HashMap<Integer,View> map = new HashMap<Integer,View>();
		       
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return Peoples.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return Peoples.get(position);
		}

		@Override
		public long getItemId(int id) {
			// TODO Auto-generated method stub
			return id;
		}
		public void  addPerson(String[] person){
			if (Peoples.get(person) == null){
				Names.add(person[0]);
				Peoples.put(person[0], person);
				notifyDataSetChanged();
				for (int i= 0 ;i <Peoples.size();i++){
					MyCheckBox.add(false);
				}
			}else {
				return ;
			}
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			// TODO Auto-generated method stub
			View listview  ;
			ViewHolder viewHolder ;

			if(map.get(position) == null){
				LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
				
				listview =  inflater.inflate(R.layout.contract_list_item, null);
				viewHolder	 = new ViewHolder();	
				viewHolder.people_name= (TextView)listview.findViewById(R.id.people_name2);
				viewHolder.people_phone = (TextView)listview.findViewById(R.id.people_phone2);
				viewHolder.checkbox = (CheckBox)listview.findViewById(R.id.check_ok);
				final int posi = position;
				viewHolder.checkbox.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View view){
						CheckBox  check = (CheckBox)view;
						MyCheckBox.set(posi, check.isChecked());
					}
				});
				listview.setTag(viewHolder);
			}else{
				listview = map.get(position);
				viewHolder = (ViewHolder)listview.getTag();
			}
			viewHolder.people_name.setText(Peoples.get(Names.get(position))[0]);
			viewHolder.people_phone.setText(Peoples.get(Names.get(position))[1]);
			viewHolder.checkbox.setSelected(MyCheckBox.get(position));
			return listview;
		}
		
	}
	
	class LoadContract extends AsyncTask<Context,Void,Context> {


		@SuppressWarnings("deprecation")
		@Override
		protected Context doInBackground(Context... params) {
			// TODO Auto-generated method stub
			ContentResolver cr  = params[0].getContentResolver();
			Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
			while(c.moveToNext()){
				String name =  c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
				String phone = null;
				
				String  select = "("+ContactsContract.Contacts.DISPLAY_NAME + "==\""+ name+ "\")";
				Cursor c_in_c = params[0].getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PEOPLE_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME+" COLLATE LOCALIZED ASC");
				startManagingCursor(c);
				if (c_in_c.moveToNext()){
					String id = c_in_c.getString(0);
					Cursor cursor = params[0].getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
				
				
					while(cursor.moveToFirst()){
						phone = (cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
					}
					String[] person = {name,phone};
					persons.add(person);

				}
								
			}
			
			return params[0];
		}
		
		@Override
		protected void onPostExecute(Context params){
			for (String[] person :persons){
				adapter.addPerson(person);
			}
			contract_list.setAdapter(adapter);
			Toast.makeText(params, "Load ok", Toast.LENGTH_SHORT).show();
			
		}
		
	}

}
